local mt = {}

function mt:save(data, config_data)
	if config_data == nil then
		config_data = {}
	end

	for n, table_data in pairs(data) do
		self:save_data(table_data, config_data[n])
	end
end

function mt:save_data(data, path)
	local function convert(tbl, stack)
		local s = {}
		for key, value in pairs(tbl) do
			if tonumber(key) == nil and key:match('([%w_]+)') ~= key then
				key = "'" .. key .. "'"
			end
			if type(value) == 'table' then
				s[#s + 1] = key .. ' = {\n' .. convert(value, (stack or 0) + 1) .. '}'
			elseif type(value) == 'string' then
				local num = tonumber(value)
				if num then
					if num == math.floor(num) then
						s[#s + 1] = key .. ' = ' .. math.floor(num)
					else
						s[#s + 1] = key .. ' = ' .. num
					end
				else
					if value == '<null>' then
						value = ''
					end
					s[#s + 1] = key .. ' = [=[' .. value .. ']=]'
				end
			else
				s[#s + 1] = key .. ' = ' .. tostring(value)
			end

			if stack ~= nil then
				s[#s + 1] = ','
			end
			s[#s + 1] = '\n'
		end
		s[#s + 1] = '\n'
		return table.concat(s)
	end
	local buffer = {}
	for name, object in pairs(data) do
		table.insert(buffer, "[" .. name .. "]\n" .. convert(object))
	end
	print('生成', path, IO.Write(path, table.concat(buffer)))
end

return mt
